﻿using log4net;

using System;
using System.Linq;
using System.Threading;
using System.Timers;
using System.Xml.Linq;

using Timer = System.Timers.Timer;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
[assembly: log4net.Config.Repository]
namespace XJWNSJSC
{
    internal class Program
    {
        private static ILog Logger;
        static void Main(string[] args)
        {


            //动态多线程
            Thread[] thread;

            ThreadStart starthread = new ThreadStart(Start);
            XDocument doc = XDocument.Load("..\\..\\setting.config"); // 加载XML文件

            var elements = from person in doc.Descendants("thread")
                               //where person.Attribute("id").Value == "2"
                           select person;

            int i = 0;
            Console.WriteLine("XML元素行数:" + elements.Count());
            thread = new Thread[elements.Count()];
            foreach (var element in elements)
            {

                //启动多线程
                thread[i] = new Thread(starthread);
                thread[i].Name = element.FirstAttribute.Value;
                thread[i].Start();
                i = i + 1;


            }
        }


        /// <summary>
        /// 多线程执行
        /// </summary>
        private static void Start()
        {
            Timer _timer;
            Timer _timer2;

            //线程名称
            string threadname = Thread.CurrentThread.Name;
            Console.WriteLine("启动:[" + threadname + "]线程！");
            InitLogger(threadname);
            Logger = LogManager.GetLogger("Process");
            Logger.Info("启动:[" + threadname + "]线程！");


            //获取配置文件信息
            //XmlDocument doc = new XmlDocument();
            //doc.Load("..\\..\\setting.config");
            //doc.de
            //XmlNode bookNode = doc.SelectSingleNode("/configuration/thread/mz/ip");
            //Console.WriteLine(bookNode.Value.ToString());
            XDocument doc = XDocument.Load("..\\..\\setting.config"); // 加载XML文件

            var elements = from table in doc.Descendants("thread")
                               .Where(e => e.Attribute("id").Value == threadname)
                           select table; // 查询特定元素

            foreach (var element in elements)
            {
                Console.WriteLine(element.Element("type").Value); // 输出元素的值
                Console.WriteLine(element.Element("ip").Value);
                Console.WriteLine(element.Element("database").Value);
                Console.WriteLine(element.Element("uid").Value);
                Console.WriteLine(element.Element("pwd").Value);
                Console.WriteLine(element.Element("totype").Value);
                Console.WriteLine(element.Element("toip").Value);
                Console.WriteLine(element.Element("todatabase").Value);
                Console.WriteLine(element.Element("touid").Value);
                Console.WriteLine(element.Element("topwd").Value);
                Console.WriteLine(element.Element("proc").Value);


                XDocument xdoc = XDocument.Parse(element.Element("proc").ToString());

                // 查询XML数据
                var query = from person in xdoc.Descendants("list")
                                //where person.Attribute("id").Value == "2"
                            select person;

                foreach (var item in query)
                {
                    Console.WriteLine($"存储过程: {item.Value}");
                }
            }




            // 创建一个定时器，设置间隔时间为2000毫秒（即2秒）
            _timer = new Timer(1000);
            // 设置Elapsed事件处理程序
            _timer.Elapsed += OnTimedEvent;


            // 启用定时器
            _timer.Enabled = true;

            Console.WriteLine(threadname + ":按任意键停止定时器...");
            Console.ReadKey();

            // 停止定时器
            _timer.Enabled = false;


            //在指定时间执行
            void OnTimedEvent(Object source, ElapsedEventArgs e)
            {
                //Console.WriteLine("定时器触发时间: {0:HH:mm:ss.fff}", e.SignalTime);
                //获取当前时间
                DateTime now = DateTime.Now; // 获取当前时间
                Console.WriteLine(threadname + ":当前的本地时间为: " + now.ToString());
                string ss = "2024-10-31 12:22:00";
                if (now >= DateTime.Parse(ss))
                {
                    Console.WriteLine(threadname + ":开始执行定时任务");
                    _timer.Enabled = false;
                    _timer2 = new Timer(10000);
                    _timer2.Elapsed += OnTimedUpload;
                    _timer2.Enabled = true;
                }
            }

            //指定时间开始后间隔时间执行
            void OnTimedUpload(Object source, ElapsedEventArgs e)
            {
                Console.WriteLine(threadname + ":定时器触发时间: {0:HH:mm:ss.fff}", e.SignalTime);
                Console.WriteLine(threadname + ":按任意键停止任务...");
                Console.ReadKey();

                // 停止定时器
                _timer2.Enabled = false;
            }
        }

        private static void InitLogger(string name)
        {
            log4net.GlobalContext.Properties["dynamicName"] = name;
        }

    }
}
